<?php  
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_ad_text.php');

class AdText
{
	var $ad_text_c;
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct()
	{
		$this->ad_text_c = new AdTextControl();
	}
	/**
	* @函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{
	
		$url_info = $ad_info = array();
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		//分页处理
		$page = 1;
		if (!empty($_REQUEST['page'])) {
			$page = (int)$_REQUEST['page'];
		}
		if($_POST['search'] || $_REQUEST['url_page'] == 1 )//搜索,分页
		{
			$search_array = array
			(
			'ad_name' => empty($_POST['ad_name'])?urldecode($_GET['ad_name']):trim($_POST['ad_name'])
			);
			$url_info = $this->ad_text_c->get_url_info($page,'list_s',20,$w_type,$search_array);
			$ad_info = $this->ad_text_c->get_ad_info('list_s',$url_info['page_info']['start'],20,$w_type,$search_array);
			
		}
		else
		{
			$url_info = $this->ad_text_c->get_url_info($page,'list_index',20,$w_type);
			$ad_info = $this->ad_text_c->get_ad_info('list_index',$url_info['page_info']['start'],20,$w_type);
		}
		$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
		$GLOBALS['smarty']->assign('ad_name',  trim($_REQUEST['ad_name']));
		$GLOBALS['smarty']->assign('w_type',   $w_type);
		$GLOBALS['smarty']->assign('size_info',   $delivery_info['size']);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('ad_info',$ad_info );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('ad_text_index.htm'); 
	
	}
	/**
	* @函数add_edit//增加，修改
	* @param  NULL
	* @return  string
	*/
	public function add_edit()
	{
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		$array = array
		(
		'ad_name' =>  trim($_POST['ad_name']),
		'ad_id' =>  trim($_POST['ad_id']),
		'ad_url' => trim($_POST['ad_url']),
		'ad_content' => trim($_POST['ad_content']),
		'ad_products_position' => trim($_POST['products_position']),
		'ad_start_products_position' => trim($_POST['start_products_position'])
		);
		
		if($_POST['sub_from'])//修改或者增加
		{
			if($_REQUEST['act'] == 'add')
			{
				$last_id  = $this->ad_text_c->operate_ad($_REQUEST['act'],$array,$w_type);
				if(!empty($last_id))
				{
				     echo  '<script type="text/javascript"> alert("增加成功！");
				    window.location.href="/wondershare/ad_text.php?action=add_edit&act=edit&w_type='.$w_type.'&ad_id='.(int)$last_id.'"; </script>';
				    
				}
			}else
			{
				//删除队列
				queueDelete(0, $_POST['ad_id']);
				
				$affect = $this->ad_text_c->operate_ad($_REQUEST['act'],$array,$w_type,(int)$_POST['ad_id']);
				if(!empty($affect))
				{
				    echo  '<script type="text/javascript"> alert("编辑成功！");
				    window.location.href="/wondershare/ad_text.php?action=add_edit&act=edit&w_type='.$w_type.'&ad_id='.(int)$_POST['ad_id'].'"; </script>';
				}
			}
			exit;
		}
		if($_REQUEST['act'] == 'add')
		{
		
			$GLOBALS['smarty']->assign('title',     '添加');
			$GLOBALS['smarty']->assign('form_act',     'add');
		
		}elseif($_REQUEST['act'] == 'edit')
		{
			//加入队列
			queueInfo($_GET['ad_id']);
			
			$ad_info = $this->ad_text_c->get_ad_id_info((int)$_GET['ad_id'],$w_type);
			
			$GLOBALS['smarty']->assign('ad_info',$ad_info );
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('title',     '修改');
		}
		elseif($_REQUEST['act'] == 'is_name_exist')//ajax判断是否有重复名称出现
		{
			$where_str = !empty($_GET['ad_id'])?' AND  ad_id != '.$_GET['ad_id']:'';
			$sql = 'SELECT count(*) AS nums  FROM ad WHERE ad_name = "'.trim($_GET['ad_name']).'"'.$where_str.' AND is_text_ad = 1 AND is_delete = 0 AND ad_web = '.$w_type;
			$nums = $GLOBALS['db']->getOne($sql);
			echo  $nums;
			exit;
		}
		
		$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
		$GLOBALS['smarty']->assign('w_type',   $w_type);
		$GLOBALS['smarty']->display('ad_text_info.htm');
	}

	/**
	* @函数del
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->ad_text_c->delete_ad($checkList);
		}
	}
	/**
	* @函数products_list
	* @param  NULL
	* @return  string
	*/
	public function products_list()
	{
		$check_row = array();
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		$ad_products_position = empty($_REQUEST['products_position'])?'':$_REQUEST['products_position'];
		$ad_id = empty($_REQUEST['ad_id'])?'':$_REQUEST['ad_id'];//广告ID
		$products_name = empty($_REQUEST['products_name'])?'':trim($_REQUEST['products_name']);//产品名称
		if($_POST['search'])
		{
			$products_list = $this->ad_text_c->get_products_list($w_type,$ad_products_position,$ad_id,$products_name);
		}else
		{
			$products_list = $this->ad_text_c->get_products_list($w_type,$ad_products_position,$ad_id);
		}
		$GLOBALS['smarty']->assign('products_name',   $products_name);
		$GLOBALS['smarty']->assign('products_position',   $ad_products_position);
		$GLOBALS['smarty']->assign('ad_id',   $ad_id);
		$GLOBALS['smarty']->assign('products_list',   $products_list);
		$GLOBALS['smarty']->assign('w_type',   $w_type);
		$GLOBALS['smarty']->display('ad_products_list.htm');
	
	}
	
}
$action    = $_REQUEST['action']; 
$ad_text   = new AdText;
if(method_exists($ad_text, $action)) 
{
	$ad_text->$action();
}else{
	$ad_text->index();
}

?>
